package cn.cloudtogo.support.redis;

import cn.cloudtogo.common.Assert;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author yaolianhua789@gmail.com
 **/
public abstract class BaseRedisOps implements RedisOps {

    protected final RedisTemplate<String, Object> template;

    public BaseRedisOps(RedisTemplate<String, Object> template) {
        this.template = template;
    }

    @Override
    public Set<String> getAllKeys() {
        return template.keys("*");
    }

    @Override
    public Long remove(Set<String> keys) {
        return template.delete(keys);
    }

    @Override
    public Boolean remove(String key) {
        Assert.notNull(key, () -> "key is null");
        return template.delete(key);
    }

    @Override
    public Boolean exists(String key) {
        Assert.notNull(key, () -> "key is null");
        return template.hasKey(key);
    }

    @Override
    public Long getExpired(String key) {
        Assert.notNull(key, () -> "key is null");
        return template.getExpire(key);
    }

    @Override
    public Long getExpired(String key, TimeUnit timeUnit) {
        Assert.notNull(key, () -> "key is null");
        return template.getExpire(key, timeUnit);
    }
}
